﻿using System.Collections.Generic.CodePlex;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace CircularTest
{
    
    
    /// <summary>
    ///This is a test class for CircularListTest and is intended
    ///to contain all CircularListTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CircularListTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Count
        ///</summary>
        public void CountTestHelper<T>()
        {
            CircularList<T> target = new CircularList<T>(3); 
            int expected = 0;
            Assert.AreEqual(expected, target.Count);
            target.Add(default(T));
            expected = 1;
            Assert.AreEqual(expected, target.Count);
            target.Add(default(T));
            expected = 2;
            Assert.AreEqual(expected, target.Count);
            target.Add(default(T));
            expected = 3;
            Assert.AreEqual(expected, target.Count);
            target.Add(default(T));
            Assert.AreEqual(expected, target.Count);
        }

        [TestMethod()]
        public void CountTest()
        {
            CountTestHelper<GenericParameterHelper>();
        }


        [TestMethod()]
        public void ItemTest()
        {
            int cap = 3; 
            CircularList<string> target = new CircularList<string>(cap); 
            target.Add("1");
            Assert.AreEqual(target[0], "1");
            target.Add("2");
            target.Add("3");
            target.Add("4");
            target.Add("5");

            Assert.AreEqual(target.Count, cap);
            Assert.AreEqual(target[0], "3");
            Assert.AreEqual(target[1], "4");
            Assert.AreEqual(target[2], "5");
        }

        [TestMethod()]
        public void RemoveAtTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);
            target.Add("1");
            target.Add("2");
            target.Add("3");
            target.Add("4");
            target.Add("5");

            target.RemoveAt(1);
            Assert.AreEqual(target.Count, cap-1);
            Assert.AreEqual(target[0], "3");
            Assert.AreEqual(target[1], "5");
            try
            {
                string alph = target[2];
                Assert.Fail();
            }
            catch
            {
               
            }
            target.Add("6");
            Assert.AreEqual(target.Count, cap);
            Assert.AreEqual(target[0], "3");
            Assert.AreEqual(target[1], "5");
            Assert.AreEqual(target[2], "6");
            target.Add("7");
            Assert.AreEqual(target[0], "5");
            Assert.AreEqual(target[1], "6");
            Assert.AreEqual(target[2], "7");
        }

        [TestMethod()]
        public void RemoveTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);
            target.Add("1");
            target.Add("1");
            target.Add("2");
            Assert.AreEqual(target.Count, cap);
            target.Remove("1");
            Assert.AreEqual(target.Count, 1);
        }

        [TestMethod()]
        public void InsertTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);
            try
            {
                target.Insert(0, "a");
                Assert.Fail();
            }
            catch 
            {
                Assert.IsTrue(true);
            }
        }

        [TestMethod()]
        public void GetEnumeratorTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);
            string expected = "234";
            string actual = string.Empty;
            target.Add("1");
            target.Add("2");
            target.Add("3");
            target.Add("4");
            foreach (string item in target)
            {
                actual += item;
            }

            Assert.AreEqual(expected, actual);
        }



        [TestMethod()]
        public void IndexOfTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);
            int expected, actual;
            
            target.Add("1");
            target.Add("2");
            target.Add("3");
            target.Add("4");

            expected = -1;
            actual = target.IndexOf("1");
            Assert.AreEqual(expected, actual);

            expected = 1;
            actual = target.IndexOf("3");
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void CopyToTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);
            string[] actual = new string[3];

            target.Add("1");
            target.Add("2");
            target.Add("3");
            target.Add("4");

            target.CopyTo(actual, 0);
            Assert.AreEqual(actual[0], "2");
            Assert.AreEqual(actual[1], "3");
            Assert.AreEqual(actual[2], "4");
        }

        [TestMethod()]
        public void ContainsTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);

            target.Add("1");
            target.Add("2");
            target.Add("3");
            target.Add("4");

            Assert.IsTrue(target.Contains("2"));
            Assert.IsFalse(target.Contains("1"));
        }


        [TestMethod()]
        public void ClearTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);

            target.Add("1");
            target.Add("2");
            target.Add("3");
            target.Add("4");

            target.Clear();
            Assert.AreEqual(target.Count,0);
            try
            {
                string a = target[0];
                Assert.Fail();
            }
            catch
            {
                Assert.IsTrue(true);
            }
        }

        [TestMethod()]
        public void AddTest()
        {
            int cap = 3;
            CircularList<string> target = new CircularList<string>(cap);
            target.Add("1");
            Assert.AreEqual(target[0], "1");
            target.Add("2");
            target.Add("3");
            target.Add("4");
            target.Add("5");

            Assert.AreEqual(target.Count, cap);
            Assert.AreEqual(target[0], "3");
            Assert.AreEqual(target[1], "4");
            Assert.AreEqual(target[2], "5");
        }
    }
}
